import Mock from 'Mock';
import Cookies from 'Cookies';

// 获取父级页面的URL
function getParentUrl() {
  if (parent !== window) {
    try {
      return parent.location.origin;
    } catch (e) {
      return document.referrer;
    }
  }
}
const isDev = process.env.NODE_ENV === 'development';
const origin = isDev ? 'http://localhost:8127' : getParentUrl();

// 发送消息给主应用
export function sendMsg2main(payload) {
  top.postMessage(payload, origin);
}

// 发送消息给主应用-显示message消息
export function showTopMessage(payload) {
  sendMsg2main({
    type: 'showTopMessage',
    params: payload,
  });
}
export default {
  install(Vue) {
    Vue.mixin({
      data() {
        return {
          Mock,
          Cookies,
        };
      },
      methods: {
        sendMsg2main,
        showTopMessage,
      },
    });
  },
};
